package com.zmn.plat.services.impl.product.businessline;

import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zmn.common.dto.StatusDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.common.constant.ProductConsts;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.product.businessline.ProductBusinessLine;
import com.zmn.plat.model.entity.product.businessline.ProductBusinessLineQuery;
import com.zmn.plat.persistence.interfaces.product.businessline.ProductBusinessLineDAO;
import com.zmn.plat.services.interfaces.product.businessline.ProductBusinessLineService;

import lombok.extern.slf4j.Slf4j;

/**
 * 描述: 产品业务线
 *
 * @author tanbiao
 * @since 2020/10/27 14:52
 **/
@Slf4j
@Service
public class ProductBusinessLineServiceImpl implements ProductBusinessLineService {
    private static final String TAG = "产品业务线服务接口";
    @Resource
    private ProductBusinessLineDAO productBusinessLineDAO;

    @Override
    public ProductBusinessLine findById(Integer relateId) {
        if (Objects.isNull(relateId)) {
            log.error("[{}],根据id查询数据relateId为null", TAG);
            return null;
        }
        return productBusinessLineDAO.findById(relateId);
    }

    @Override
    public List<ProductBusinessLine> listByIdList(List<Integer> relateIdList) {
        if (CollectionUtil.isNullOrEmpty(relateIdList)) {
            log.error("nameList null error, relateIdList:{}", relateIdList);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL, "产品组ID集合不能为空");
        }

        return productBusinessLineDAO.listByIdList(relateIdList);
    }

    @Override
    public List<ProductBusinessLine> listPageByQuery(ProductBusinessLineQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }

        int count = this.countByQuery(query);
        query.setTotalCount(count);

        return productBusinessLineDAO.listPageByQuery(query);
    }

    @Override
    public List<ProductBusinessLine> listByQuery(ProductBusinessLineQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return productBusinessLineDAO.listByQuery(query);
    }

    @Override
    public List<ProductBusinessLine> listByNameListAndBizType(Integer bizType, List<String> nameList) {
        if (NumberUtil.isNullOrZero(bizType)) {
            log.error("bizType null error, bizType:{}", bizType);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL, "业务类型不能为空");
        }

        if (bizType < ProductConsts.BIZ_TYPE_C || bizType > ProductConsts.BIZ_TYPE_B) {
            log.error("bizType out of range error, bizType:{},errorCode:{}", bizType, PlatErrorCodeDict.ERROR_CODE_PARAM_OUT_OF_RANGE_ERROR);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PARAM_OUT_OF_RANGE_ERROR, "业务类型取值超出范围");
        }

        if (CollectionUtil.isNullOrEmpty(nameList)) {
            log.error("nameList null error, nameList:{},errorCode:{}", nameList, PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL, "产品业务线名称不能为空");
        }

        return productBusinessLineDAO.listByNameListAndBizType(bizType, nameList);
    }

    @Override
    public Integer countByQuery(ProductBusinessLineQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return productBusinessLineDAO.countByQuery(query);
    }

    @Override
    public void updateSortByKey(Integer relateId, Integer sort, String updater) {
        ProductBusinessLine productBusinessLine = new ProductBusinessLine();
        productBusinessLine.setRelateId(relateId);
        productBusinessLine.setSort(sort);
        productBusinessLine.setUpdater(updater);
        productBusinessLine.setUpdateTime(DateUtil.getNow());
        productBusinessLineDAO.updateSortByKey(productBusinessLine);
    }

    @Override
    public Integer updateByKey(ProductBusinessLine productBusinessLine) {
        if (Objects.isNull(productBusinessLine.getRelateId())) {
            log.error("[{}],根据id修改数据,productGroup为null", TAG);
            return null;
        }
        return productBusinessLineDAO.updateByKey(productBusinessLine);
    }

    @Override
    public Integer updateStatusBatch(StatusDTO dto) {
        return productBusinessLineDAO.updateStatusBatch(dto);
    }

    @Override
    public Integer insert(ProductBusinessLine productBusinessLine) {
        if (Objects.isNull(productBusinessLine)) {
            log.error("[{}],新增数据,productGroup为null", TAG);
            return null;
        }
        return productBusinessLineDAO.insert(productBusinessLine);
    }

    @Override
    public Integer deleteByKey(Integer relateId) {

        if (Objects.isNull(relateId)) {
            log.error("[{}],根据id删除数据,groupId为null", TAG);
            return null;
        }
        return productBusinessLineDAO.deleteByKey(relateId);
    }
}
